package www.wxwnb.clerk.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;
import www.wxwnb.clerk.R;
import www.wxwnb.clerk.adapter.AlreadyCheckedAdapter;
import www.wxwnb.clerk.base.BaseFragment;
import www.wxwnb.clerk.bean.AlreadyCheckBean;
import www.wxwnb.clerk.ui.activity.AccountOpenActivity;
import www.wxwnb.clerk.ui.api.ApiServer;
import www.wxwnb.clerk.ui.api.MyRetrofit;
import www.wxwnb.clerk.utils.Customer;
import www.wxwnb.clerk.utils.UrlConnect;

import static cn.jpush.android.api.JPushInterface.a.l;

/**
 * Created by lenovo on 2017/9/9.
 */

public class AlreadyCheckedFragment extends BaseFragment{

    private View view;
    private RecyclerView mRecyclerView;
    private AccountOpenActivity activity;
    private AlreadyCheckedAdapter adapter;


    private Handler handler=new Handler();
    List<AlreadyCheckBean.DataBean.PageRecordsBean> list=new ArrayList<>();
    private int index=0;
    private String jobNumber;
    private ListView mListView;
    private SpringView mSpringView;

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(view==null){
           view = inflater.inflate(R.layout.already_check,null);
        }
        activity = (AccountOpenActivity) getActivity();
        jobNumber = Customer.getInstance().getJobNumber();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mRecyclerView = view.findViewById(R.id.recycler_already_c);
        mSpringView = view.findViewById(R.id.springView);
        mSpringView.setHeader(new DefaultHeader(activity));
        mSpringView.setFooter(new DefaultHeader(activity));
        mSpringView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                list.clear();
                index=1;
                sendRequest(jobNumber,index);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSpringView.onFinishFreshAndLoad();
                    }
                },20000);
            }

            @Override
            public void onLoadmore() {
              //  list.clear();
                index++;
                Log.i("index",index+"");
              sendRequest(jobNumber,index);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                         mSpringView.onFinishFreshAndLoad();
                    }
                },2000);
            }
        });
        if(list.size()==0){
            //请求数据
            sendRequest(jobNumber,1);
            index=1;
        }

    }

    private void sendRequest(String jobNumber,int pageNum) {
        Log.i("indexssss",pageNum+"");
        Flowable<AlreadyCheckBean> flowable = MyRetrofit.getIntence().getRetrofit(UrlConnect.BASE_URL).create(ApiServer.class).alreadyChecked(jobNumber, pageNum);
        flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<AlreadyCheckBean>() {
                    @Override
                    public void onNext(AlreadyCheckBean alreadyCheckBean) {
                        if(alreadyCheckBean.getReturnCode()==1){
                            AlreadyCheckBean.DataBean data = alreadyCheckBean.getData();
                            List<AlreadyCheckBean.DataBean.PageRecordsBean> beanList = data.getPageRecords();
                            for (int i = 0; i <beanList.size() ; i++) {
                                list.add(beanList.get(i));
                            }
                            Log.i("listDatas",list.size()+list.toString());
                            //设置适配器
                            setAdapterDatas(list);
                            mSpringView.getFooterView().setVisibility(View.GONE);

                        }else{
                            //设置适配器
                            setAdapterDatas(list);
                            Log.i("listDatas",list.size()+list.toString());

                        }
                    }
                    @Override
                    public void onError(Throwable t) {
                        mSpringView.setVisibility(View.GONE);
                        mSpringView.getFooterView().setVisibility(View.GONE);
                        Toast.makeText(activity, "网络异常，请稍后再试", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void initListener() {

    }
    @Override
    public void initData() {


    }
    private void setAdapterDatas(List<AlreadyCheckBean.DataBean.PageRecordsBean> beanList){
     if(adapter==null){
            adapter = new AlreadyCheckedAdapter(activity,beanList, 0);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false));
            mRecyclerView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }


}
